<template>
  <el-dialog
    :title="importPatentObj.title"
    append-to-body
    :visible.sync="importPatentObj.show"
    :close-on-click-modal="false"
    width="900px"
  >
    <el-row>
      <upload-excel
        importText="导入专利信息"
        :on-success="generateData"
      ></upload-excel>
      <el-button type="success" @click="downloadClick" size="mini"
        >下载模板</el-button
      >
    </el-row>
    <div class="mb-10" style="color:red;margin-left:40px;">
      注意：导入时请保证客户名称存在，同一专利申请号不重复，日期格式（
      如：2020-01-01）单元格为文本格式，专利类型请填写“发明”、“实用新型”或“外观设计”！
    </div>
    <el-table :data="dataList" border style="width: 100%;" size="mini">
      <el-table-column align="center" width="50" type="index"></el-table-column>
      <el-table-column
        v-for="(item, index) in dataTableHeader"
        :prop="item.prop"
        align="center"
        :label="item.label"
        :width="item.width"
        :key="index"
        :show-overflow-tooltip="'' || item.tooltiop"
      ></el-table-column>
    </el-table>
    <div class="mb-10" style="color:red;margin-left:40px;">
      {{ this.message }}
    </div>
  </el-dialog>
</template>
<script>
import { placeholderTips } from "@u/validate";
// import formatExcelDate from "@u/timeFormat";
import UploadExcel from "@/components/UploadExcel";
import { importPatentApi } from "@s/api";
export default {
  name: "dialogImportPatent",
  data() {
    return {
      placeholderTips: placeholderTips,
      dataList: [],
      message: "",
      dataTableHeader: [
        { prop: "applicationNumber", label: "客户名称", width: "" },
        { prop: "title", label: "专利名称", width: "" },
        { prop: "applicationTypeShow", label: "专利类型", width: "" },
        { prop: "applicationNumber", label: "申请号", width: "" },
        { prop: "applicationDatetime", label: "申请日", width: "" },
        { prop: "applicant", label: "申请人", width: "" }
      ],
      tabHeader: []
    };
  },
  props: ["importPatentObj"],
  components: { UploadExcel },
  methods: {
    initDialog() {
      this.dataList = [];
      this.message = "";
    },
    generateData({ header, results }) {
      this.message = "";
      this.tabHeader = header;

      let json = [];

      results.forEach(item => {
        // 接收参数
        var newItem = {
          customerName: String(item[this.tabHeader[0]]),
          title: String(item[this.tabHeader[1]]),
          applicationTypeShow: String(item[this.tabHeader[2]]),
          applicationType: "",
          applicationNumber: String(item[this.tabHeader[3]]),
          applicationDatetime: item[this.tabHeader[4]]
            ? item[this.tabHeader[4]]
            : "",
          country: item[this.tabHeader[5]] ? item[this.tabHeader[5]] : "",
          publicNumber: item[this.tabHeader[6]] ? item[this.tabHeader[6]] : "",
          publicDatetime: item[this.tabHeader[7]]
            ? item[this.tabHeader[7]]
            : "",
          authorizeNoticeNumber: item[this.tabHeader[8]]
            ? item[this.tabHeader[8]]
            : "",
          authorizeNoticeDatetime: item[this.tabHeader[9]]
            ? item[this.tabHeader[9]]
            : "",
          classinfoNumber: item[this.tabHeader[10]]
            ? item[this.tabHeader[10]]
            : "",
          applicant: item[this.tabHeader[11]] ? item[this.tabHeader[11]] : "",
          applicantAddress: item[this.tabHeader[12]]
            ? item[this.tabHeader[12]]
            : "",
          inventor: item[this.tabHeader[13]] ? item[this.tabHeader[13]] : "",
          agent: item[this.tabHeader[14]] ? item[this.tabHeader[14]] : "",
          agency: item[this.tabHeader[15]] ? item[this.tabHeader[15]] : "",
          classification: item[this.tabHeader[16]]
            ? item[this.tabHeader[16]]
            : "",
          description: item[this.tabHeader[17]] ? item[this.tabHeader[17]] : "",
          patentAbstract: item[this.tabHeader[18]]
            ? item[this.tabHeader[18]]
            : "",
          mainClaims: item[this.tabHeader[19]] ? item[this.tabHeader[19]] : "",
          projectImage: item[this.tabHeader[20]]
            ? item[this.tabHeader[20]]
            : "",
          pdfUrl: item[this.tabHeader[21]] ? item[this.tabHeader[21]] : "",
          legalDatetime1: item[this.tabHeader[22]]
            ? item[this.tabHeader[22]]
            : "",
          legalInfo1: item[this.tabHeader[23]] ? item[this.tabHeader[23]] : "",
          legalDesc1: item[this.tabHeader[24]] ? item[this.tabHeader[24]] : "",
          legalDatetime2: item[this.tabHeader[25]]
            ? item[this.tabHeader[25]]
            : "",
          legalInfo2: item[this.tabHeader[26]] ? item[this.tabHeader[26]] : "",
          legalDesc2: item[this.tabHeader[27]] ? item[this.tabHeader[27]] : "",
          legalDatetime3: item[this.tabHeader[28]]
            ? item[this.tabHeader[28]]
            : "",
          legalInfo3: item[this.tabHeader[29]] ? item[this.tabHeader[29]] : "",
          legalDesc3: item[this.tabHeader[30]] ? item[this.tabHeader[30]] : "",
          legalDatetime4: item[this.tabHeader[31]]
            ? item[this.tabHeader[31]]
            : "",
          legalInfo4: item[this.tabHeader[32]] ? item[this.tabHeader[32]] : "",
          legalDesc4: item[this.tabHeader[33]] ? item[this.tabHeader[33]] : "",
          legalDatetime5: item[this.tabHeader[34]]
            ? item[this.tabHeader[34]]
            : "",
          legalInfo5: item[this.tabHeader[35]] ? item[this.tabHeader[35]] : "",
          legalDesc5: item[this.tabHeader[36]] ? item[this.tabHeader[36]] : "",
          legalList: []
        };
        json.push(newItem);
      });
      // 数据校验
      for (var i = 0; i < json.length; i++) {
        // 必填校验
        var item = json[i];
        if (
          item.customerName == "" ||
          item.title == "" ||
          item.applicationNumber == "" ||
          item.applicationTypeShow == "" ||
          item.applicationDatetime == ""
        ) {
          this.message =
            "客户名称、专利名称、专利类型、申请号及申请日必须填写！";
          return;
        }
        // 数据校验
        if (!this.validatePatentNumber(item.applicationNumber)) {
          this.message = "导入失败：专利申请号请输入数字为主的编号！";
          return;
        }
        if (
          item.applicationTypeShow != "发明" &&
          item.applicationTypeShow != "实用新型" &&
          item.applicationTypeShow != "外观设计"
        ) {
          this.message = "专利类型请填写“发明”、“实用新型”或“外观设计”！";
          return;
        } else {
          if (item.applicationTypeShow == "发明") {
            item.applicationType = "1";
          } else if (item.applicationTypeShow == "实用新型") {
            item.applicationType = "2";
          } else if (item.applicationTypeShow == "外观设计") {
            item.applicationType = "3";
          }
        }
        // 整理法律状态数据
        if (item.legalDatetime1 && item.legalInfo1) {
          var legalItem1 = {
            legalDatetime: item.legalDatetime1,
            legalInfo: item.legalInfo1,
            legalDesc: item.legalDesc1
          };
          item.legalList.push(legalItem1);
        }
        if (item.legalDatetime2 && item.legalInfo2) {
          var legalItem2 = {
            legalDatetime: item.legalDatetime2,
            legalInfo: item.legalInfo2,
            legalDesc: item.legalDesc2
          };
          item.legalList.push(legalItem2);
        }
        if (item.legalDatetime3 && item.legalInfo3) {
          var legalItem3 = {
            legalDatetime: item.legalDatetime3,
            legalInfo: item.legalInfo3,
            legalDesc: item.legalDesc3
          };
          item.legalList.push(legalItem3);
        }
        if (item.legalDatetime4 && item.legalInfo4) {
          var legalItem4 = {
            legalDatetime: item.legalDatetime4,
            legalInfo: item.legalInfo4,
            legalDesc: item.legalDesc4
          };
          item.legalList.push(legalItem4);
        }
        if (item.legalDatetime5 && item.legalInfo5) {
          var legalItem5 = {
            legalDatetime: item.legalDatetime5,
            legalInfo: item.legalInfo5,
            legalDesc: item.legalDesc5
          };
          item.legalList.push(legalItem5);
        }
      }
      json = JSON.parse(JSON.stringify(json));
      window.console.log(json);
      importPatentApi(json).then(res => {
        if (!res.code && res.length == 0) {
          this.$message({
            type: "success",
            message: "导入成功！"
          });
          this.dataList = JSON.parse(JSON.stringify(json));
          this.$emit("reflash");
        } else if (!res.code && res.length > 0) {
          this.message = "导入失败：";
          for (var j = 0; j < res.length; j++) {
            this.message = this.message + res[j] + ";";
          }
        }
      });
    },
    /*限制  专利申请号 */
    validatePatentNumber(value) {
      // var reg = /^(\d+|\d+\.\d{1,2})$/;  // 含0
      let reg = /^[a-zA-Z0-9_.-]{4,50}$/;
      if (value && !reg.test(value)) {
        return false;
      } else {
        return true;
      }
    },
    downloadClick() {
      window.open(
        "https://patent-manage.oss-cn-shanghai.aliyuncs.com/template/%E4%B8%93%E5%88%A9%E4%BF%A1%E6%81%AF%E5%AF%BC%E5%85%A5%E6%A8%A1%E6%9D%BF.xls"
      );
    }
  },
  watch: {
    "importFeeObj.show"(val) {
      if (val) {
        this.initDialog();
      }
    }
  }
};
</script>
<style lang="scss" scoped></style>
